package com.zsy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zsy.entity.Official;
import com.zsy.entity.Plan;
import com.zsy.entity.Research;
import com.zsy.mapper.IResearchMapper;
import com.zsy.service.IResearchService;
import com.zsy.utils.PageBean;
import com.zsy.utils.Result;
//import com.zsy.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ResearchServiceImpl implements IResearchService {
    @Autowired
    IResearchMapper irm;

    //判断申请表是否填写完整
    @Override
    public Result insertResearch(Research research) {

        if (research.getProjectName() == null) {
            return Result.error("项目名称不能为空");
        }
        if (research.getUrgencyLevel() == null) {
            return Result.error("积极程度不能为空");
        }
        if (research.getRdCost() == null) {
            return Result.error("研发成本总计不能为空");
        }
        if (research.getEstimatedHeadCount() == null) {
            return Result.error("研发人数不能为空");
        }
        if (research.getRdCity() == null) {
            return Result.error("研发城市不能为空");
        }
        if (research.getDetailedAddress() == null) {
            return Result.error("详细地址不能为空");
        }
        if (research.getRdStage() == null) {
            return Result.error("研发阶段不能为空");
        }
        if (research.getRdType() == null) {
            return Result.error("研发类型不能为空");
        }

        int i = irm.addResearch(research);
        if (i > 0){
            return Result.ok("申请成功");
        }else {
            return Result.error("申请失败");
        }

    }
//查询数据
    @Override
    public Result selectRes(Research research) {
//        Map<String, Object> map = ThreadLocalUtil.get();
//        String dp = (String) map.get("department");
//        if (dp.equals("研发部") || dp.equals("管理员")) {
            return Result.ok(irm.selectResearch(research));
//        }
//        else {
//            return Result.error("错误");
//        }

    }


    //根据给的参数查询数据与条数，返回一个list
    @Override
    public Result selectResearch(Research research) {






        research.setPage((research.getPage() - 1) * research.getIndex());
        // 获取限制的记录
        List<Research> limit = irm.getlimit(research);
       // 检查是否有数据
        if (limit != null && !limit.isEmpty()) {
            System.out.println("2");
            // 如果有数据，执行查询

            int i = irm.selectResearchQuantity(research);

            List<Object> all = new ArrayList<>();

            all.add(i);
            all.add(limit);
            System.out.println("分页查询"+all);
            return Result.ok(all);
        } else {
            // 如果没有数据，返回错误信息
            return Result.error("无数据");
        }
    }
    //    删除数据根据id，
    @Override
    public Result deleteResearch(Integer[] researchId) {

        System.out.println(researchId[0]);
        if (researchId == null)
            return Result.error("删除失败");
        int i = irm.deleteResearch(researchId);
        if (i>-1){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
//修改数据
    @Override
    public Result updateResearch(Research research) {

        System.out.println(research);
        int i = irm.updateResearch(research);
        if (i>0){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");
        }

    }


    @Override
    public Result insertPlan(Plan plan) {

        if (plan.getTopic() ==null){
            return Result.error("主题不能为空");
        }
        if (plan.getHost() ==null){
            return Result.error("主办人不能为空");
        }

        if (plan.getStartDate()==null){
            return Result.error("开始时间不能为空");
        }
        if (plan.getEndDate()==null){
            return Result.error("结束时间不能为空");
        }
        if (plan.getDetails()==null)
            return Result.error("详情不能为空");
        int i = irm.addPlan(plan);
        if (i>0){
            return Result.ok("添加成功");
        }else {
            return Result.error("添加失败");
        }

    }

    @Override
    public Result deletePlan(Integer[] planId) {

        System.out.println(planId[0]);
        if (planId == null)
            return Result.error("删除失败");
        int i = irm.deletePlan(planId);
        if (i >-1){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");

    }

    //查询研发计划
    @Override
    public Result selectPlan(Plan plan) {

        return Result.ok(irm.selectPlan(plan));

    }

    @Override
    public Result updatePlan(Plan plan) {

        int i = irm.updatePlan(plan);
        if (i>0){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");

        }
    }


    //添加文案
    @Override
    public Result addOffice(Official official) {

//         判断输入的字段是否为空
        if (official.getTitle()==null)
            return Result.error("标题不能为空");
        if (official.getAuthor()==null)
            return Result.error("作者不能为空");
        if (official.getCategory()==null)
            return Result.error("分类不能为空");
        if (official.getContent()==null)
            return Result.error("内容不能为空");
        int i = irm.addOfficial(official);
        if (i>0){
            return Result.ok("添加成功");
        }else {
            return Result.error("添加失败");
        }


    }

    @Override
    public Result selectOfficial(Official official) {

        return Result.ok(irm.selectOfficial(official));


        
    }

    @Override
    public Result delofficlal(Integer[] officialId) {
//        Map<String, Object> map = ThreadLocalUtil.get();
//        String dp = (String) map.get("department");
//        if (dp.equals("研发部") || dp.equals("管理员")) {
        System.out.println(officialId[0]);
        if (officialId == null)
            return Result.error("删除失败");
        int i = irm.delofficlal(officialId);
        if (i>-1){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
//                }
//        else {
//            return Result.error("权限不足");
//        }


    }

    @Override
    public PageBean<Research> list(Integer pageNum, Integer pageSize,
                                   Integer estimatedHeadcount,
                                   String rdCity,
                                   String rdType) {

        System.out.println("service:"+estimatedHeadcount + " " + rdCity + " " + rdType);
        // 1.创建PageBean对象
        PageBean<Research> pb = new PageBean<>();
        // 2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);
        // 3.调用mapper
        //Map<String,Object> map = ThreadLocalUtil.get();
        //Integer userId = (Integer) map.get("id");
        List<Research> as = irm.list(estimatedHeadcount, rdCity, rdType);
        // Page中提供了方法，可获取PageHelper分页查询后，得到的总条数和当前页数据
        Page<Research> p = (Page<Research>) as;
        // 把数据填到PageBean中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }




}

//研发计划
